package com.ulife.order.components.core.refund.factory;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.rpc.RpcContext;
import com.google.common.base.Strings;
import com.ulife.base.entities.order.SaleOrderPay;
import com.ulife.base.entities.order.SoRefundBill;
import com.ulife.base.logics.order.SaleOrderPayLogic;
import com.ulife.common.gateway.utils.define.CommonParameter;
import com.ulife.common.gateway.utils.define.Version;
import com.ulife.member.api.MemberService;
import com.ulife.order.api.core.refund.Channels;
import com.ulife.order.api.core.refund.RefundBillStatus;
import com.ulife.order.api.core.refund.RefundStatus;
import com.ulife.order.components.core.result.BaseResult;
import com.ulife.order.components.core.result.ResultCode;
import com.ulife.order.service.enumeration.PayStatus;
import com.ulife.member.api.response.common.Result;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.DecimalFormat;

/**
 * Created by Lbq on 2016/05/26.
 */
@Component(value = "balanceRefundFactory")
public class BalanceRefundFactory implements IRefundFactory {

    @Resource
    SaleOrderPayLogic saleOrderPayLogic;
    @Reference(version = Version.LATEST)
    MemberService memberService;
    @Override
    public BaseResult processRefund(SoRefundBill soRefundBill,long operator) {

        BaseResult result = new BaseResult();
        SaleOrderPay saleOrderPay = saleOrderPayLogic.get(soRefundBill.getSoPayId());
        if (saleOrderPay == null || saleOrderPay.getStatus() != PayStatus.Success.value()) {
            result.setMessage("支付记录不存在或支付状态不正确");
            return result;
        }
        if(soRefundBill.getStatus() == RefundBillStatus.Finished.value()
                || soRefundBill.getRefundStatus() == RefundStatus.Succeed.value()){
            result.setMessage("退款单据已经完成退款操作");
            result.setCode(ResultCode.Repeat);
            return result;
        }
        if(Strings.isNullOrEmpty(soRefundBill.getRefundChannel())|| !Channels.Balance.value().equals(soRefundBill.getRefundChannel())){
            result.setMessage("退款渠道为空,或渠道不是Balance");
            result.setCode(ResultCode.Failure);
            return result;
        }
        try{

            RpcContext.getContext().setAttachment(CommonParameter.userId, Long.toString(operator));
            Result mResult =  memberService.refundBalance(soRefundBill.getSoNo(), soRefundBill.getSoPayId().toString());
            result.setMessage(mResult.getMessage());
            if(mResult.isSuccess()){
                result.setCode(ResultCode.Success);
                result.setData(RefundStatus.Succeed);
                //您的订单退款X元已退款到您的余额/微信/支付宝（注：这里显示实际退款方式）账号，请注意查收。如有问题请致电400-687-909
                result.setMessage("您的订单退款"
                        .concat(new DecimalFormat("####0.00").format(soRefundBill.getRefundAmount()))
                        .concat("元已退款到您的")
                        //.concat(saleOrderPay.getPayChannel())
                        .concat("余额账号，请注意查收。如有问题请致电400-687-909"));
            }
        }catch (Exception ex){
            result.setCode(ResultCode.Failure);
            result.setMessage(ex.getMessage());
        }
        return result;
    }
}
